﻿using System;
using System.Data;
using TopBuilds.Database;

namespace TopBuilds.Restaurant.Data
{
    public class TableCategoryObject
    {
        public static TableCategory[] GetTableCategories()
        {
            return Query(null);
        }

        public static TableCategory GetCategory(int id)
        {
            TableCategory category = new TableCategory();

            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(TableCategoryDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = TableCategoryDesc.TableName;
            string sql = builder.QueryCommandLine;

            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);

            category.Id                  = Convert.ToInt32(table.Rows[0][TableCategoryDesc.Id]);
            category.Name                = Convert.ToString(table.Rows[0][TableCategoryDesc.Name]);
            category.MinConsumption      = Convert.ToInt32(table.Rows[0][TableCategoryDesc.MinConsumption]);
            category.State               = Convert.ToInt32(table.Rows[0][TableCategoryDesc.State]);
            category.Remark              = Convert.ToString(table.Rows[0][TableCategoryDesc.Remark]);

            return category;
        }

        public static TableCategory GetTableCategory(string name)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(TableCategoryDesc.Name, ConditionExpress.EQUALS, name);

            TableCategory[] set = Query(builder.ConditionString);
            if (set != null && set.Length > 0)
            {
                return set[0];
            }

            return null;
        }

        public static TableCategory[] Query(string condition)
        {
            // query factories from database
            string sql = "select * from " + TableCategoryDesc.TableName;
            if (condition != null && condition.Length > 0)
            {
                sql += " where ";
            }
            DataTable table = DatabaseObject.Instance.ExecuteQuerySQL(sql);


            // rebuild the data
            if (table != null && table.Rows.Count > 0)
            {
                TableCategory[] cagegories = new TableCategory[table.Rows.Count];

                for (int i = 0; i < table.Rows.Count; i++)
                {
                    TableCategory category = new TableCategory();

                    category.Id                  = Convert.ToInt32(table.Rows[i][TableCategoryDesc.Id]);
                    category.Name                = Convert.ToString(table.Rows[i][TableCategoryDesc.Name]);
                    category.MinConsumption      = Convert.ToInt32(table.Rows[i][TableCategoryDesc.MinConsumption]);
                    category.State               = Convert.ToInt32(table.Rows[i][TableCategoryDesc.State]);
                    category.Remark              = Convert.ToString(table.Rows[i][TableCategoryDesc.Remark]);

                    cagegories.SetValue(category, i);
                }

                return cagegories;
            }
            return null;
        }

        public static Int32 Add(TableCategory tableCategory)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.InsertAdd(TableCategoryDesc.Name,                     tableCategory.Name);
            builder.InsertAdd(TableCategoryDesc.MinConsumption,           tableCategory.MinConsumption);
            builder.InsertAdd(TableCategoryDesc.State,                    tableCategory.State);
            builder.InsertAdd(TableCategoryDesc.Remark,                   tableCategory.Remark);
            builder.QueryTableName = TableCategoryDesc.TableName;

            string sql = builder.InsertCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }

        public static bool Delete(Int32 id)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.ConditionAdd(TableCategoryDesc.Id, ConditionExpress.EQUALS, id);
            builder.QueryTableName = TableCategoryDesc.TableName;
            string sql = builder.DeleteCommandLine;

            return (DatabaseObject.Instance.ExecuteNonQuerySQL(sql) != -1);
        }

        public static Int32 Update(TableCategory tableCategory)
        {
            SqlBuilder builder = new SqlBuilder();
            builder.UpdateAdd(TableCategoryDesc.Name,                    tableCategory.Name);
            builder.UpdateAdd(TableCategoryDesc.MinConsumption,          tableCategory.MinConsumption);
            builder.UpdateAdd(TableCategoryDesc.State,                   tableCategory.State);
            builder.UpdateAdd(TableCategoryDesc.Remark,                  tableCategory.Remark);
            builder.ConditionAdd(TableCategoryDesc.Id, ConditionExpress.EQUALS, tableCategory.Id);
            builder.QueryTableName = TableCategoryDesc.TableName;

            string sql = builder.UpdateCommandLine;

            return DatabaseObject.Instance.ExecuteNonQuerySQL(sql);
        }
    }
}
